package cn.yzgy.yzgyoa.common.service.impl;

import cn.yzgy.yzgyoa.common.constant.YzCacheName;
import cn.yzgy.yzgyoa.common.dto.SysDictValueDto;
import cn.yzgy.yzgyoa.common.entity.SysDictValue;
import cn.yzgy.yzgyoa.common.mapper.SysDictValueMapper;
import cn.yzgy.yzgyoa.common.service.ISysDictValueService;
import cn.yzgy.yzgyoa.common.vo.DictKeyValueVo;
import cn.yzgy.yzgyoa.ucenter.entity.UserInfoDetail;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Yqsen
 * @since 2021-07-14
 */
@Slf4j
@Service
@CacheConfig(cacheNames = YzCacheName.SYS_DICT_CACHE_NAME)
public class SysDictValueServiceImpl extends ServiceImpl<SysDictValueMapper, SysDictValue> implements ISysDictValueService {

    @Override
    public IPage<SysDictValueDto> getAllDictValue(Page<SysDictValueDto> dictValuePage, SysDictValueDto queryDto) {
        return baseMapper.getAllDictValue(dictValuePage,queryDto);
    }

    @Override
    public int updateDictValue(UserInfoDetail userInfoDetail,SysDictValue queryDto) {
        queryDto.setUpdateBy(userInfoDetail.getUid()).setUpdateTime(LocalDateTime.now());
        return baseMapper.updateById(queryDto);
    }

    public int updateDictValueList(UserInfoDetail userInfoDetail,SysDictValue queryDto) {
        queryDto.setUpdateBy(userInfoDetail.getUid()).setUpdateTime(LocalDateTime.now());
        return baseMapper.updateDictValueList(queryDto);
    }

    @Override
    public boolean createDictValue(UserInfoDetail userInfoDetail, SysDictValue queryDto) {
        queryDto.setCreateBy(userInfoDetail.getUid()).setCreateTime(LocalDateTime.now());
        return queryDto.insert();
    }

    @Override
    public Object getDictValue(String dictCode) {
        return baseMapper.selectList(new QueryWrapper<SysDictValue>().lambda()
                    .eq(SysDictValue::getDictCode,dictCode)
                    .orderByAsc(SysDictValue::getDictItemSort)).stream()
                    .map(DictKeyValueVo::trans)
                    .collect(Collectors.toList());
    }

    /**
     * 获取dictCode字典的dictValue对应的显示数据
     * @param dictCode 字典编码
     * @param dictValue 编码对应的值
     * @return 字典显示数据
     */
    public String getValue(String dictCode, String dictValue) {
        if (dictCode == null || dictValue == null || "".equals(dictValue)) {
            return "";
        }
        SysDictValue value = getOne(new LambdaQueryWrapper<SysDictValue>()
                .eq(SysDictValue::getDictCode, dictCode)
                .eq(SysDictValue::getDictItemValue, dictValue));
        if (value != null) {
            return value.getDictItemText();
        }
        return "";
    }

    /**
     * 获取字典值
     * @param dictCode 字典码
     * @param dictValue 要查询的值
     * @return 值或null
     */
    @Cacheable(keyGenerator = YzCacheName.GENERATOR_NAME)
    public String getDictValue(String dictCode, String dictValue) {
        String res = getValue(dictCode, dictValue);
        return "".equals(res) ? null : res;
    }
}
